﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using elementManagement;
using helpers;

namespace productsMarket
{
    public class Atoms
    {

        private static Atoms instance = new Atoms();
        private StreamReader inFile;
        private List<Element> elements;
        private Parameters parameters;
        private Element element;
        
        private Atoms()
        {    
            inFile = new StreamReader(FoldersHelper.resolveFilePath("atoms.txt"));
            elements = new List<Element>();
            populateAtomsList();
            
        }

        private void populateAtomsList()
        {
            string line;
            string[] tokens; 
            while ((line = inFile.ReadLine()) != null) {
                if (line.StartsWith("//") || line.Trim().Length == 0)
                {
                    continue;
                }
                tokens = line.Split(' ');
                element = new Element();
                parameters = new Parameters();

                parseLine(tokens);

                element.parameters = parameters;
                elements.Add(element);
            }
            inFile.Close();
        }

        private void parseLine(string[] tokens)
        {
            element.name = tokens[0];
            try
            {
                parameters.consumption = Decimal.Parse(tokens[1]);
                parameters.efficiency = Decimal.Parse(tokens[2]);
                parameters.health = Decimal.Parse(tokens[3]);
                parameters.mass = Decimal.Parse(tokens[4]);
                parameters.acceleraion = Decimal.Parse(tokens[5]);
                parameters.protection = Decimal.Parse(tokens[6]);
                parameters.power = Decimal.Parse(tokens[7]);
                parameters.degradationRate = Int32.Parse(tokens[8]);                
            }
            catch (FormatException fe)
            {
                Console.WriteLine(fe.StackTrace);
            }
        }

        public static Atoms getInstance() 
        {
            return instance;
        }

        public List<Element> getElements()
        {
            return elements;
        }
    }    
}
